﻿using System;
using System.Collections.Generic;
using System.Net.Mail;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
using MailMessage = System.Net.Mail.MailMessage;

public partial class Test_Default : System.Web.UI.Page
{
    protected void Page_Load(object sender, EventArgs e)
    {

    }
    /// <summary>
    /// method to send email using SMTP server
    /// </summary>
    /// <param name="_FromEmail">From email address</param>
    /// <param name="_FromName">From name</param>
    /// <param name="_ToEmail">To email address</param>
    /// <param name="_ToName">To name</param>
    /// <param name="_Subject">Email subject</param>
    /// <param name="_EmailBody">Email message body</param>
    /// <param name="_IsBodyHtml">Is email message body HTML</param>
    /// <param name="_Attachments">Email message file attachments</param>
    /// <param name="_EmailServer">SMTP email server address</param>
    /// <param name="_LoginName">SMTP email server login name</param>
    /// <param name="_LoginPassword">SMTP email server login password</param>
    /// <returns>TRUE if the email sent successfully, FALSE otherwise</returns>
    public bool SendEmail(string _FromEmail, string _FromName, string _ToEmail, string _ToName, string _Subject, string _EmailBody, bool _IsBodyHtml, string[] _Attachments, string _EmailServer, string _LoginName, string _LoginPassword)
    {
        try
        {
            // setup email header
            System.Net.Mail.MailMessage _MailMessage = new System.Net.Mail.MailMessage();

            // Set the message sender
            // sets the from address for this e-mail message.
            _MailMessage.From = new System.Net.Mail.MailAddress("support@parkerpen.com.vn");
            // Sets the address collection that contains the recipients of this e-mail message.
            _MailMessage.To.Add(new System.Net.Mail.MailAddress("kiennnit@gmail.com"));

            // sets the message subject.
            _MailMessage.Subject = _Subject;
            // sets the message body.
            _MailMessage.Body = _EmailBody;
            // sets a value indicating whether the mail message body is in Html.
            // if this is false then ContentType of the Body content is "text/plain".
            _MailMessage.IsBodyHtml = _IsBodyHtml;

            // add all the file attachments if we have any
            //if (_Attachments != null && _Attachments.Length > 0)
            //    foreach (string _Attachment in _Attachments)
            //        _MailMessage.Attachments.Add(new System.Net.Mail.Attachment(_Attachment));

            // SmtpClient Class Allows applications to send e-mail by using the Simple Mail Transfer Protocol (SMTP).
            System.Net.Mail.SmtpClient _SmtpClient = new System.Net.Mail.SmtpClient("mail.parkerpen.com.vn");

            //Specifies how email messages are delivered. Here Email is sent through the network to an SMTP server.
            //_SmtpClient.DeliveryMethod = System.Net.Mail.SmtpDeliveryMethod.Network;
            // Some SMTP server will require that you first authenticate against the server.
            // Provides credentials for password-based authentication schemes such as basic, digest, NTLM, and Kerberos authentication.
            System.Net.NetworkCredential _NetworkCredential = new System.Net.NetworkCredential(_LoginName, _LoginPassword);
            _SmtpClient.UseDefaultCredentials = false;
            _SmtpClient.Credentials = _NetworkCredential;
            //Let's send it
            _SmtpClient.EnableSsl = false;
            //_SmtpClient.Send(_MailMessage);

            // Do cleanup
            _MailMessage.Dispose();
            _SmtpClient = null;
        }
        catch (Exception _Exception)
        {
            // Error
            Console.WriteLine("Exception caught in process: {0}", _Exception.ToString());
            return false;
        }

        return true;
    }
    protected void Button1_Click(object sender, EventArgs e)
    {
        SMTP sm=new SMTP();
        bool kq = sm.SendEmail("support@parkerpen.cọm.vn", "kiennnit@gmail.com", "Này thì ghử mail subject", "<b>Này thì thử bođi</b><br/>Dòng mới trong body nè",
                     true, "mail.parkerpen.com.vn", "support@parkerpen.com.vn", "123321");
        if(kq)
        {
            lblKQ.Text = "Thành công";
        }
        else
        {
            lblKQ.Text = "Mother of Thành công :(";
        }
        SendEmail("support@parkerpen.cọm.vn", "support@parkerpen.cọm.vn", "kiennnit@gmail.com", "kiennnit@gmail.com", "Test phát",
               "Body email test phát", false, new string[2], "mail.parkerpen.com.vn", "support@parkerpen.com.vn", "123321");
        //MailAddress SendTo = new MailAddress("kiennnit@gmail.com");
        //MailAddress SendFrom = new MailAddress("support@parkerpen.com.vn");
        //MailMessage MyMessage = new MailMessage(SendFrom, SendTo);

        //MyMessage.Subject = "Này thì súp jếc";
        //MyMessage.Body = "Này thì thằng bođy";


        //SmtpClient emailClient = new SmtpClient("mail.parkerpen.com.vn");

        //// add bừa xem thế oéo nào
        //emailClient.Port = 3000;
        //SmtpServer oServer = new SmtpServer("smtp.emailarchitect.net");

        //// User and password for ESMTP authentication, if your server doesn't require
        //// User authentication, please remove the following codes.
        //oServer.User = "test@emailarchitect.net";
        //oServer.Password = "testpassword"; 

        //emailClient.Send(MyMessage);

    }

}
